//
//  NSString+Size.m
//  jobClient
//
//  Created by 一览iOS on 16/5/17.
//  Copyright © 2016年 YL1001. All rights reserved.
//

#import "NSString+Size.h"

@implementation NSString (Size)

-(CGSize)sizeNewWithFont:(UIFont *)font constrainedToSize:(CGSize)size{
    NSDictionary *attributes = @{NSFontAttributeName: font};
    CGRect rect = [self boundingRectWithSize:size
                                     options:NSStringDrawingTruncatesLastVisibleLine |
                   NSStringDrawingUsesLineFragmentOrigin |
                   NSStringDrawingUsesFontLeading
                                           attributes:attributes
                                              context:nil];
//    CGSize rect = [self sizeWithFont:font constrainedToSize:size];
    return rect.size;
}

-(CGSize)sizeNewWithFont:(UIFont *)font{
    CGSize textSize = [self sizeWithAttributes:@{ NSFontAttributeName :font}];
    return textSize;
}

- (CGSize)sizeNewWithFont:(UIFont *)font constrainedToSize:(CGSize)size lineBreakMode:(NSLineBreakMode)lineBreakMode{
   return [self sizeNewWithFont:font constrainedToSize:size];
}

- (CGSize)sizeNewWithFont:(UIFont *)font forWidth:(CGFloat)width lineBreakMode:(NSLineBreakMode)lineBreakMode{
    return [self sizeNewWithFont:font constrainedToSize:CGSizeMake(width,10000000)];
}

-(NSMutableAttributedString *)getHtmlAttString {
    NSString *selfStr = self;
    if ([selfStr containsString:@"&apos;"]) {
        NSArray *strArr = [selfStr componentsSeparatedByString:@"&apos;"];
        selfStr = [strArr componentsJoinedByString:@"'"];
    }
    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[selfStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
    return attrStr;
}

/*
 - (NSMutableAttributedString *)getHtmlAttString {
 NSString *selfStr = [self replaceHtmlStr];
 //    selfStr = [selfStr stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
 //    NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithData:[selfStr dataUsingEncoding:NSUnicodeStringEncoding] options:@{ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType } documentAttributes:nil error:nil];
 NSMutableAttributedString * attrStr = [[NSMutableAttributedString alloc] initWithString:selfStr];
 return attrStr;
 }
 
 - (NSString *)replaceHtmlStr {
 NSString *selfStr = self;
 if ([selfStr containsString:@"&nbsp;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&nbsp;"];
 selfStr = [strArr componentsJoinedByString:@" "];
 }
 if ([selfStr containsString:@"&lt;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&lt;"];
 selfStr = [strArr componentsJoinedByString:@"<"];
 }
 if ([selfStr containsString:@"&gt;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&gt;"];
 selfStr = [strArr componentsJoinedByString:@">"];
 }
 if ([selfStr containsString:@"&amp;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&amp;"];
 selfStr = [strArr componentsJoinedByString:@"&"];
 }
 if ([selfStr containsString:@"amp;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"amp;"];
 selfStr = [strArr componentsJoinedByString:@"&"];
 }
 if ([selfStr containsString:@"&quot;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&quot;"];
 selfStr = [strArr componentsJoinedByString:@"\""];
 }
 if ([selfStr containsString:@"&apos;"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"&apos;"];
 selfStr = [strArr componentsJoinedByString:@"'"];
 }
 if ([selfStr containsString:@"<br/>"]) {
 NSArray *strArr = [selfStr componentsSeparatedByString:@"<br/>"];
 selfStr = [strArr componentsJoinedByString:@""];
 }
 return selfStr;
 }

 */

-(NSMutableAttributedString *)getHtmlAttStringWithFont:(UIFont *)font color:(UIColor *)color{
    NSMutableAttributedString * attrStr = [self getHtmlAttString];
    if (font){
        [attrStr addAttribute:NSFontAttributeName value:font range:NSMakeRange(0,attrStr.length)];
    }
    if (color){
        [attrStr addAttribute:NSForegroundColorAttributeName value:color range:NSMakeRange(0,attrStr.length)];
    }
    return attrStr;
}

//反转
-(NSMutableString*)Reverse
{
    const char *str = [self UTF8String];
    NSUInteger length = [self length];
    char *pReverse = (char*)malloc(length+1);//动态分配空间
    strcpy(pReverse, str);
    for(int i=0; i<length/2; i++)
    {
        char c = pReverse[i];
        pReverse[i] = pReverse[length-i-1];
        pReverse[length-i-1] = c;
    }
    NSMutableString *pstr = [NSMutableString stringWithUTF8String:pReverse];
    free(pReverse);
    return pstr;
}

-(NSDate *)dateFormStringFormat:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]];
    [formatter setDateFormat:format];
    return [formatter dateFromString:self];
}

-(NSDate *)dateFormStringCurrentLocaleFormat:(NSString *)format{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]];
    [formatter setDateFormat:format];
    return [formatter dateFromString:self];
}

-(NSDate *)dateFormStringFormat:(NSString *)format timeZone:(NSTimeZone *)timeZone{
    NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
    [formatter setLocale:[[NSLocale alloc] initWithLocaleIdentifier:@"zh_cn"]];
    [formatter setTimeZone:timeZone];
    [formatter setDateFormat:format];
    return [formatter dateFromString:self];
}

-(NSString *)priceStringFormat{
    NSString *priceStr = @"";
    NSString *oldStr = self;
    if ([self hasSuffix:@"."]){
        oldStr = [oldStr substringWithRange:NSMakeRange(0,oldStr.length-1)];
    }
    NSArray *arr = [oldStr componentsSeparatedByString:@"."];
    if(arr.count == 1){
        priceStr = [NSString stringWithFormat:@"%zd",[arr[0] integerValue]];
    }else if(arr.count == 2){
        NSString *arr0 = [NSString stringWithFormat:@"%zd",[arr[0] integerValue]];
        NSString *arr1 = arr[1];
        if ([arr1 integerValue] > 0) {
            for (NSInteger i = arr1.length-1;i>=0;i--) {
                NSString *str = [arr1 substringWithRange:NSMakeRange(i,1)];
                if (![str isEqualToString:@"0"]) {
                    arr1 = [arr1 substringWithRange:NSMakeRange(0,i+1)];
                    break;
                }
            }
            priceStr = [NSString stringWithFormat:@"%@.%@",arr0,arr1];
        }else{
            priceStr = arr0;
        }
    }
    return priceStr;
}

-(NSString *)scanWithStart:(NSString *)start end:(NSString *)end{
    NSScanner * scanner = [NSScanner scannerWithString:self];
    NSString * text = nil;
    while([scanner isAtEnd]==NO)
    {
        //找到标签的起始位置
        [scanner scanUpToString:start intoString:nil];
        //找到标签的结束位置
        [scanner scanUpToString:end intoString:&text];
        //替换字符
    }
    text = [text stringByReplacingOccurrencesOfString:start withString:@""];
    return text;
}

-(NSMutableAttributedString *)getLineSpacing:(CGFloat)space{
    NSMutableAttributedString *string1 = [[NSMutableAttributedString alloc]initWithString:self];
    NSMutableParagraphStyle *paragraphStyle = [[NSMutableParagraphStyle alloc]init];
    [paragraphStyle setLineSpacing:space];
    [string1 addAttribute:NSParagraphStyleAttributeName value:paragraphStyle range:NSMakeRange(0,string1.length)];
    return string1;
}

@end
